package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorsMapper;

    /**
     * 分页查询菜品
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //开启分页插件
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //排除两端空格
        if (StringUtils.isNotBlank(dishPageQueryDTO.getName())) {
            dishPageQueryDTO.setName(dishPageQueryDTO.getName().trim());
        }
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }


    /**
     * 新增菜品和口味数据
     * @param dishDTO
     * @return
     */
    @Override
    public void saveDishAndFlvors(DishDTO dishDTO) {
        //添加菜品数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //注意在mapper中生成主键id
        dishMapper.insert(dish);
        //获取该菜品的id
        Long dishId = dish.getId();
        //获取口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //如果有口味数据就设置菜品id
        if (flavors != null && flavors.size()>0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            //批量添加口味
            dishFlavorsMapper.insertBatchFlavors(flavors);
        }

    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @Override
    public void deleteBatch(List<Integer> ids) {
        //删除菜品
        dishMapper.deleteBatch(ids);

    }

    /**
     * 回显菜品数据
     * @param id
     * @return
     */
    @Override
    public DishVO getDishById(Long id) {
        //根据菜品id查询菜品数据
        DishVO dishVO = dishMapper.getDishById(id);
        //根据菜品id 来查询口味数据
        List<DishFlavor> dishFlavors = dishFlavorsMapper.getFlavorsByDishId(id);
        //将口味数据设置到菜品表中
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @Override
    public void updateWithFlavors(DishDTO dishDTO) {
        //创建菜品实体类
        Dish dish = new Dish();
        //将前端数据拷贝进菜品实体类
        BeanUtils.copyProperties(dishDTO, dish);
        //修改菜品的基本信息
        dishMapper.update(dish);

        //删除原有的菜品 中口味数据
        dishFlavorsMapper.deleteByDishId(dishDTO.getId());
        //获取菜品中的口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //判断口味是否有数据
        if (flavors != null && flavors.size() > 0) {
            //将设置的口味添加到该菜品中
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            //添加口味数据
            dishFlavorsMapper.insertBatchFlavors(flavors);
        }
    }

    /**
     * 修改菜品状态
     * @param id
     * @param status
     * @return
     */
    @Override
    public void updateWithStatus(Long id,Integer status ) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);
    }


    /**
     * 根据分类id查询菜品数据
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> getDishsByCategoryId(Long categoryId) {
        List<Dish> dishList = dishMapper.getDishsByCategoryId(categoryId);
        return dishList;
    }

    /**
     * 根据菜品id获取菜品状态
     * @param ids
     * @return
     */
    @Override
    public Integer getStatusByIds(List<Integer> ids) {
        Integer statusCount = dishMapper.getStatusByIds(ids);
        return statusCount;
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        Long categoryId = dish.getCategoryId();
        List<Dish> dishList = dishMapper.getDishsByCategoryId(categoryId);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorsMapper.getFlavorsByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;

    }
}
